<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="i-ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
          >批量操作
          <Icon icon="i-mdi:chevron-down" />
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <FileRecordDesc @register="registerDesc" />
</template>
<script lang="ts" name="FileRecord" setup>
  import { ref } from "vue"
  import { downloadByOnlineUrl } from "/@/utils/file/download"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useMessage } from "/@/hooks/web/useMessage"
  import FileRecordDesc from "./DetailDrawer.vue"
  import { columns, searchFormSchema } from "./fileRecord.data"
  import { list, deleteFileRecord, batchDeleteFileRecord } from "./fileRecord.api"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { Icon } from "/@/components/Icon"

  const showFooter = ref(true)
  const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer()
  const { createMessage } = useMessage()
  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: "file-record",
    tableProps: {
      title: "文件记录列表",
      api: list,
      showIndexColumn: true,
      rowSelectionTipType: "onTableTitle",
      striped: false,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      bordered: false,
      actionColumn: {
        width: 140,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
  })
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

  /**
   * 详情
   */
  function handleDetail(record) {
    showFooter.value = false
    openRoleDesc(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteFileRecord({ id: record.id }, reload)
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteFileRecord({ ids: selectedRowKeys.value }, reload)
  }

  const handleDownload = (record) => {
    downloadByOnlineUrl(record.fileUrl, record.originalName)
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      },
      {
        label: "下载",
        ifShow: !record.isPrivate,
        onClick: handleDownload.bind(null, record),
      },
      {
        label: "删除",
        color: "error",
        ifShow: !record.isPrivate,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
</script>
